package com.ds.lens.data.common.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ds.lens.data.common.client.vo.LensMetricInVO;
import com.ds.lens.data.common.util.HttpServiceClient;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class DynamicMetricQueryClientWrapper implements DynamicMetricQueryClient {

    private DynamicMetricQueryClient dynamicMetricQueryClient;
    private static final String message = "error occured.";
    private Gson gson = new GsonBuilder().create();

    private <T> LensResponseData<T> createErrorResponse(Exception e) {
        log.error(message, e);
        LensResponseData resp = new LensResponseData<>();
        resp.setCode(1);
        resp.setMessage(e.getMessage());
        return resp;
    }

    public DynamicMetricQueryClientWrapper(String monitorUrl) {
        dynamicMetricQueryClient = HttpServiceClient.getRestClient(DynamicMetricQueryClient.class, monitorUrl);
    }

    @Override
    public Map<String, Map<String, Map<String, Object>>> queryMetrics(LensMetricInVO metricInVO) {
        try {
            return dynamicMetricQueryClient.queryMetrics(metricInVO);
        } catch (Exception e) {
            log.error("error occurred while calling dynamic detect interface input is " + gson.toJson(metricInVO), e);
            return null;
        }
    }
}
